package com.bdqn.system.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bdqn.system.entity.Standard;
import com.bdqn.system.service.StandardSrevice;

@Controller
public class StandardController {

	@Autowired
	private StandardSrevice standardService;
	
	
	//查询详细列表
	@RequestMapping("/List.html")
	public ModelAndView  goStandList(String name){
		
		
		System.out.println(name);
		ModelAndView mv=new ModelAndView();
		mv.setViewName("getList");
		
		//分页基本参数
	  	int pageSize = 3;
	  	int pageCount = 0;
	  	int totalCount = 0;
	  
	  	//获取用户分页数据
		totalCount = standardService.getTotalCount(name);
	  	pageCount = (totalCount+pageSize-1)/pageSize;
	  	
	  	int pageNo = 1;
	  	int page=(pageNo-1)*pageSize;
	  	List<Standard> stands =standardService.findByPage(name, page, pageSize);
	  	mv.addObject("stands",stands);
	  	mv.addObject("pageSize", pageSize);
	  	mv.addObject("totalCount", totalCount);
	  	mv.addObject("pageCount", pageCount);
	  	mv.addObject("pageNo", pageNo);
	  	mv.addObject("name", name);
		System.out.println(stands);
		
		return mv;
	}
	
	
	@RequestMapping("/addStand.html")
	public ModelAndView addStand(String stdNum,String zhname,String version,String keys,String releaseDate,String implDate,String packagePath){
		ModelAndView mv=new ModelAndView();
		mv.setViewName("/List.html");
		System.out.println(stdNum);
		Date rDate=null;
		Date imDate=null;
		if(releaseDate!=null && !releaseDate.equals("")){
			SimpleDateFormat smd=new SimpleDateFormat("yyyy-MM-dd");
			try {
				rDate=smd.parse(releaseDate);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(implDate!=null && !implDate.equals("")){
			SimpleDateFormat smd=new SimpleDateFormat("yyyy-MM-dd");
			try {
				imDate=smd.parse(implDate);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		Standard sd=new Standard();
		sd.setImplDate(imDate);
		sd.setStdNum(stdNum);
		sd.setKeys(keys);
		sd.setPackagePath(packagePath);
		sd.setReleaseDate(rDate);
		sd.setVersion(version);
		sd.setZhname(zhname);
		System.out.println(sd);
		int i= standardService.addStand(sd);
		
		return mv;
	}
	
	@RequestMapping("/deleteStand.html")
	public ModelAndView deleteStand(int id){
		
		ModelAndView mv=new ModelAndView();
		mv.setViewName("List.html");
		
		int j=standardService.deleteById(id);
		
		return mv;
	}
	
	

	public StandardSrevice getStandardService() {
		return standardService;
	}

	public void setStandardService(StandardSrevice standardService) {
		this.standardService = standardService;
	}
	
	
}
